package hz.mobileproject.pda.firesafety.view;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.TextView;

import com.blankj.utilcode.util.StringUtils;
import com.blankj.utilcode.util.ToastUtils;

import org.xutils.view.annotation.Event;
import org.xutils.view.annotation.ViewInject;

import java.util.ArrayList;
import java.util.List;

import hz.mobileproject.pda.R;
import hz.mobileproject.pda.entity.Photo;
import hz.mobileproject.pda.firesafety.bean.Rp_queryfire;
import hz.mobileproject.pda.ui.adapter.DeptCheckGVAdapter;
import hz.mobileproject.pda.ui.view.BaseCustomDialog;
import hz.mobileproject.pda.ui.view.PhotoDialog;
import hz.mobileproject.pda.utils.TakePhotoUtil;

/**
 * Created by Lc on 2018/3/1.
 */

public class FireSafetyCallingDialog extends BaseCustomDialog implements AdapterView.OnItemClickListener, AdapterView.OnItemLongClickListener {
    @ViewInject(R.id.tv_qicai)
    private TextView tv_name;
    @ViewInject(R.id.tv_is_guoqi)
    private TextView tv_is_guoqi;
    @ViewInject(R.id.et_err_pop_question)
    private EditText et_err_pop_question;
    @ViewInject(R.id.gv_err_pop_photo)
    private GridView gv_err_pop_photo;

    private DeptCheckGVAdapter mAdapter;
    private Rp_queryfire mQueryfire;
    private String[] mGuoqiSlectors = {"过期", "未过期", "异常"};
    private OnSaveClickListner mListener;

    public FireSafetyCallingDialog(Context context, Rp_queryfire queryfire) {
        super(context);
        mQueryfire = queryfire;
    }

    @Override
    protected int bindView() {
        return R.layout.dialog_fire_calling;
    }

    @Override
    protected void initData() {
        mAdapter = new DeptCheckGVAdapter(mContext, new ArrayList<Photo>());
        gv_err_pop_photo.setAdapter(mAdapter);
        gv_err_pop_photo.setOnItemClickListener(this);
        gv_err_pop_photo.setOnItemLongClickListener(this);
        tv_name.setText(mQueryfire.getName());
    }

    @Event({R.id.tv_is_guoqi, R.id.btn_pop_save, R.id.btn_pop_cancel})
    private void viewOnClick(View v) {
        switch (v.getId()) {
            case R.id.tv_is_guoqi:
                AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
                builder.setItems(mGuoqiSlectors, new OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        tv_is_guoqi.setText(mGuoqiSlectors[i]);
                    }
                }).create().show();
                break;
            case R.id.btn_pop_cancel:
                this.dismiss();
                break;
            case R.id.btn_pop_save:
                if (StringUtils.isEmpty(tv_is_guoqi.getText().toString())) {
                    ToastUtils.showLong("请选择是否过期");
                    return;
                }
                if (StringUtils.isEmpty(et_err_pop_question.getText().toString())) {
                    ToastUtils.showLong("请填写问题说明");
                    return;
                }
                if (mListener != null) {
                    mListener.onSaveClick(tv_is_guoqi.getText() + " " + et_err_pop_question.getText(), mAdapter.getListData());
                }
                break;
        }
    }


    /**
     * gridview 点击事件
     *
     * @param adapterView
     * @param view
     * @param i
     * @param l
     */
    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
        //当点击下标大于等于list size时   为添加照片
        if (i >= mAdapter.getListData().size()) {
            AlertDialog.Builder dialog = new AlertDialog.Builder(mContext);
            final CharSequence[] items = {"拍照", "从相册中选择"};
            dialog.setItems(items, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    if (which == 0) {
                        TakePhotoUtil.getInstance((Activity) mContext).takePhoto();
                    } else if (which == 1) {
                        TakePhotoUtil.getInstance((Activity) mContext).openPic();
                    }
                }
            });
            //显示提示框
            dialog.show();
        } else {
            //点击下标小于list sizi时  查看照片
            Photo photo = mAdapter.getItem(i);
            PhotoDialog dialog = new PhotoDialog(mContext, photo.getPath());
            dialog.show();
        }
    }

    @Override
    public boolean onItemLongClick(AdapterView<?> adapterView, View view, final int position, long l) {
        if(position < mAdapter.getListData().size()){
            AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
            builder.setTitle("提示").setMessage("确认删除此照片？")
                    .setPositiveButton("确认", new OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    mAdapter.deleteOne(position);
                }
            }).setNegativeButton("取消",null).create().show();
        }
        return true;
    }

    public interface OnSaveClickListner {
        void onSaveClick(String question, List<Photo> photos);
    }

    public FireSafetyCallingDialog setmListener(OnSaveClickListner mListener) {
        this.mListener = mListener;
        return this;
    }

    public DeptCheckGVAdapter getmAdapter() {
        return mAdapter;
    }
}
